﻿using System;
using System.Collections.Generic;
using System.Text;

namespace EasyNet
{
    static internal class Packet
    {
        internal delegate byte[] Encryptor(byte[] byte_array);
        static internal byte[] CreateByteArrayPKT(byte[] byte_array, Encryptor encryptor_)
        {
            byte[] packet = CreatePKT(PacketType.ByteArray, byte_array, encryptor_);//包
            return packet;
        }
        static internal byte[] CreateTextPKT(string text_, Encoding encoding_, Encryptor encryptor_)
        {
            byte[] data = encryptor_(encoding_.GetBytes(text_));//包数据
            byte[] packet = CreatePKT(PacketType.Text, data, encryptor_);//包
            return packet;
        }
        static internal byte[] CreateFileInfoPKT(string file_name, long file_size, Encryptor encryptor_)
        {
            byte[] fileNameLength;//文件名字节数组长度
            byte[] fileName = Encoding.UTF8.GetBytes(file_name);//文件名字节数组
            fileNameLength = BitConverter.GetBytes(fileName.Length);
            byte[] fileSize = BitConverter.GetBytes(file_size);//文件大小字节数组
            byte[] data = new byte[fileName.Length + fileNameLength.Length + fileSize.Length];
            fileNameLength.CopyTo(data, 0);
            fileName.CopyTo(data, fileNameLength.Length);
            fileSize.CopyTo(data, fileNameLength.Length + fileName.Length);
            data = encryptor_(data);//包数据
            byte[] packet = CreatePKT(PacketType.FileInfo, data, encryptor_);//包
            return packet;
        }
        static internal byte[] CreateFilePKT(byte[] file_byte_array, Encryptor encryptor_)
        {
            byte[] packet = CreatePKT(PacketType.File, file_byte_array, encryptor_);//包
            return packet;
        }
        static internal byte[] CreatePKT(PacketType packetType, byte[] data_, Encryptor encryptor_)
        {
            //byte[] type = new byte[] { (byte)packetType };//包类型
            //byte[] data = encryptor_(data_);//包数据
            //byte[] length = BitConverter.GetBytes(data.Length);//包长度
            //byte[] packet = new byte[type.Length + length.Length + data.Length];//包
            //type.CopyTo(packet, 0);
            //length.CopyTo(packet, type.Length);
            //data.CopyTo(packet, type.Length + length.Length);
            //return packet;
            return data_;
        }
    }
}
